package com.sxj.corejava.code17_模板模式;

import java.util.Arrays;

public class Test {

    public static void main(String[] args) {
        // 如果传递的是数字,则根据数值大小进行降序排序
        Integer[] arr = {6, 2, 9, 14, 1, 8, 3};
        SortTemplate.sort(arr, new SortComparator<Integer>() {

            @Override
            public int compareTo(Integer t1, Integer t2) {
                return t2 - t1;
            }
        });
        System.out.println(Arrays.toString(arr));
        SortTemplate.sort(arr, (t1, t2) -> (Integer) t1 - (Integer) t2);
        System.out.println(Arrays.toString(arr));

        // 如果传递的是字符串,根据字符串长度进行升序排序
        String[] names = {"admin", "tom", "lucy", "zhangsan", "wangwu"};
        SortTemplate.sort(names, new SortComparator<String>() {
            @Override
            public int compareTo(String t1, String t2) {
                return t1.length() - t2.length();
            }
        });
        System.out.println(Arrays.toString(names));
        SortTemplate.sort(names, (t1, t2) -> ((String) t2).length() - ((String) t1).length());
        System.out.println(Arrays.toString(names));
    }
}
